﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class CellAlarm_ReasonD : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Dep_Bind();
            Auth();
            SetPager();
        }
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetReason();
        ReasonPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = ReasonPager.PageSize;
        pds.CurrentPageIndex = ReasonPager.CurrentPageIndex - 1;
        ReasonList.DataSource = pds;
        ReasonList.DataBind();
    }

    private DataSet GetReason()
    {
        string sqlstr = @"select cell_chname,start_time,recover_time,cell_class,main_reason,IsTele,Remark,rid,CELL_NET
                                    ,second_reason,report_date,cell_net from U_UNLINK_REASON_T where status = '上报' ";
        if (Dep_A.SelectedValue != "0")
        {
            sqlstr += " and CELL_CHNAME like '" + Dep_A.SelectedValue + "%'";
        }
        if (Cell_CHName_A.Text.Trim().Length > 0)
        {
            sqlstr += " and CELL_CHNAME like '%" + Tools.ReplaceString(Cell_CHName_A.Text.Trim(), 50) + "%'";
        }
        if (Main_Reason.SelectedValue.Length > 0)
        {
            sqlstr += " and main_reason = '" + Main_Reason.SelectedValue+ "'";
        }
        if (CELL_NET.SelectedValue != "0")
        {
            sqlstr += " and CELL_NET = '" + CELL_NET.SelectedValue + "'";
        }
        sqlstr += " order by start_time";
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null);
    }

    private void Dep_Bind()
    {
        string sqlstr = "SELECT DISTINCT SUBSTR(Replace(CELL_CHNAME,'G',''),0,2) AS County FROM CELL_CLASS ORDER BY SUBSTR(Replace(CELL_CHNAME,'G',''),0,2)";
        Dep_A.DataSource = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null).Tables[0].DefaultView;
        Dep_A.DataTextField = "County";
        Dep_A.DataValueField = "County";
        Dep_A.DataBind();
        Dep_A.Items.Insert(0, new ListItem("全市", "0"));
    }

    private void Auth()
    {
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            Dep_A.SelectedValue = dr["company"].ToString();
        }
        else
        {
            Dep_A.Enabled = true;
        }
    }

    protected void QueryReason_Click(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void ReasonPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void Approval_Click(object sender, EventArgs e)
    {
        ActionDo("通过");
    }

    protected void Overrule_Click(object sender, EventArgs e)
    {
        ActionDo("驳回");
    }

    private void ActionDo(string status)
    {
        string[] rids = null;
        string ridStr = HidRID.Value.Trim().TrimEnd(',');
        if (ridStr.Length > 0)
        {
            rids = ridStr.Split(',');
        }
        HidRID.Value = "";
        string sqlStr = "UPDATE U_UNLINK_REASON_T SET Status = '" + status 
            + "',EXAMINER=:EXAMINER,EXAM_DATE=SYSDATE,OVERREASON=:OVERREASON WHERE RID=:RID";
        OracleParameter[] _params = {
            new OracleParameter(":RID",OracleType.Number,18),
            new OracleParameter(":EXAMINER",OracleType.VarChar,16),
            new OracleParameter(":OVERREASON",OracleType.NVarChar,16)
        };
        _params[1].Value = LoginUser;
        _params[2].Value = HidOverReason.Value;
        for (int i = 0; i < rids.Length; i++)
        {
            _params[0].Value = rids[i];
            OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
        }
        JQueryAlert("审核完成！");        
        SetPager();
    }

    protected void ReasonList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D5E5EA'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    protected void Excel_Click(object sender, EventArgs e)
    {
        DataSet ds = GetReason();
        if (ds.Tables[0].Rows.Count > 65535)
        {
            JQueryAlert("断站原因总数已超过EXCEL最大限制65535行！\\n请缩小查询范围后再尝试导出！");
            return;
        }
        ReasonList.DataSource = ds;
        ReasonList.AllowPaging = false;
        ReasonList.DataBind();
        Tools.DGToExcel(ReasonList, Dep_A.Enabled ? "UTF-8" : "GB2312");
        SetPager();
    }
}
